from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect,Http404
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

from learing_logs.models import Topic,Entry,Blog
from learing_logs.forms import TopicForm,EntryForm,BlogForm

# Create your views here.

def index(request): #<WSGIRequest: GET '/'>
    ''' 学习笔记主页 '''
    
    return render(request,'learing_logs/index.html') #渲染模板，所有模板文件都需要放在 templates 文件夹下

@login_required #设置访问权限 只有登录的用户可以访问
def topics(request):
    topics = Topic.objects.filter(owner=request.user).order_by('date_added') #根据创建时间获取所有主题
    context = {"topics":topics}  
    return render(request,"learing_logs/topics.html",context)

@login_required 
def topic(request,topic_id): #视图函数的参数名必须和 url 定义的一样
    topic = Topic.objects.get(id=topic_id)
    if topic.owner != request.user:
        return Http404
    entries = topic.entry_set.order_by("date_added")
    context = {"topic":topic,'entries':entries}
    return render(request,"learing_logs/topic.html",context)

@login_required
def blogs(request,user_id):
    user = User.objects.get(id=user_id)
    blogs = Blog.objects.filter(owner=user).order_by('date_added')
    context = {'blogs':blogs}
   
    return render(request,'learing_logs/blogs.html',context)

def blog(request,blog_id):
    blog = Blog.objects.get(id=blog_id)
    context = {'blog':blog}
    return render(request,'learing_logs/blog.html',context)

@login_required
def new_blog(request):
    if request.method != 'POST':
        form = BlogForm()
    else:
        form = BlogForm(data=request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.owner = request.user
            user.save()
            
            return HttpResponseRedirect(reverse('learing_logs:blog',args=[form.instance.id]))
    context = {'form':form}
    return render(request,'learing_logs/new_blog.html',context)

def edit_blog(request,blog_id):
    blog = Blog.objects.get(id=blog_id)
    if request.method != 'POST':
        form = BlogForm(instance=blog)
    else:
        form = BlogForm(instance=blog,data=request.POST) #表单实例的instance属性是model实例
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('learing_logs:blog',args=[form.instance.id]))
    context = {'form':form,'is_edit':True}
    return render(request,'learing_logs/new_blog.html',context)

@login_required
def del_blog(request,blog_id):
    Blog.objects.filter(id=blog_id).delete()
    return HttpResponseRedirect(reverse('learing_logs:blogs',args=[request.user.id]))

@login_required 
def new_topic(request):    
    if request.method != 'POST':
        form = TopicForm() #访问页面 则生成一个表单
    else:
        form = TopicForm(request.POST) #提交表单 对提交的进行处理
        if form.is_valid():
            #form.save()
            
            topic = form.save(commit=False) #commit=false 知道不要马上修改数据库
            topic.owner = request.user
            topic.save() #真正提交到数据库            
            return HttpResponseRedirect(reverse("learing_logs:topics")) #reverse 函数把视图转换成 相对路径 /topics/

    context = {'form':form}
    return render(request,"learing_logs/new_topic.html",context)

@login_required 
def new_entry(request,topic_id):
    topic = Topic.objects.get(id=topic_id)
    if topic.owner != request.user:
        return Http404

    if request.method != 'POST':
        form = EntryForm()
    else:
        form = EntryForm(data=request.POST)
        if form.is_valid():
            added_entry = form.save(commit=False)
            added_entry.topic = topic
            added_entry.save()
            return HttpResponseRedirect(reverse("learing_logs:topic",args=[topic_id]))
    context = {"form":form,'topic':topic}

    return render(request,'learing_logs/new_entry.html',context)

@login_required 
def edit_entry(request,entry_id):
    ''' 编辑已有条目 '''
    print("--------edit_entry---------")
    entry = Entry.objects.get(id=entry_id)
    topic = entry.topic
    if request.method != "POST":
        form = EntryForm(instance=entry)
    else:
        #命名参数 instance=entry 自动填充表单的数据
        form = EntryForm(instance=entry,data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("learing_logs:topic"),args=[topic.id])
    context = {'entry':entry,'topic':topic,'form':form}
    return render(request,"learing_logs/edit_entry.html",context)


